package com.example.papermanagemant.controller;

import com.example.papermanagemant.model.Paper;
import com.example.papermanagemant.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
public class PaperController {

    @Autowired
    private PaperService paperService;

    @GetMapping("/papers")
    public String listPapers(@RequestParam(required = false) String search, Model model) {
        List<Paper> papers;
        if (search != null && !search.isEmpty()) {
            papers = paperService.searchPapers(search);  // 调用搜索方法
        } else {
            papers = paperService.findAll();  // 显示所有论文
        }
        model.addAttribute("papers", papers);
        model.addAttribute("search", search);  // 将搜索词传递到前端
        return "papers";
    }


    // 显示添加论文页面
    @GetMapping("/papers/add")
    public String showAddPaperPage() {
        return "add-paper"; // 显示添加论文页面
    }

    // 添加论文
    @PostMapping("/papers")
    public String addPaper(Paper paper) {
        if (paper.getId() != null) {
            paperService.save(paper);
        } else {
            // 设置上传日期为当前时间
            paper.setPubdate(new Date());
            paperService.save(paper);
        }
        return "redirect:/papers"; // 添加完成后跳转到论文列表页
    }

    // 删除论文
    @PostMapping("/papers/delete")
    public String deletePaper(@RequestParam Long id) {
        paperService.delete(id);
        return "redirect:/papers";
    }

    // 编辑论文
    @GetMapping("/papers/edit/{id}")
    public String editPaper(@PathVariable Long id, Model model) {
        model.addAttribute("paper", paperService.findAll().stream().filter(p -> p.getId().equals(id)).findFirst().orElse(null));
        return "edit-paper";
    }

    // 论文详情页面
    @GetMapping("/papers/detail/{id}")
    public String paperDetail(@PathVariable Long id, Model model) {
        Paper paper = paperService.findAll().stream().filter(p -> p.getId().equals(id)).findFirst().orElse(null);
        if (paper != null) {
            model.addAttribute("paper", paper);
            return "paper-detail"; // 论文详情页视图
        }
        return "redirect:/papers"; // 如果没有找到论文，返回论文列表页
    }

    @GetMapping("/home")
    public String dashboard(Model model) {
        long paperCount = paperService.getPaperCount();
        String allKeywords = paperService.getAllKeywords();

        // 日志输出
        System.out.println("Total Papers Count: " + paperCount);
        System.out.println("Keywords: " + allKeywords);

        model.addAttribute("paperCount", paperCount);
        model.addAttribute("allKeywords", allKeywords);

        return "home";  // 返回首页视图
    }

}
